﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;

using VitalShining.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.Admin
{
    [Function("Admin.UserManage")]
    public partial class UserManage : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlHelper.BindRoleList(chlRoles, EmptyType.SelectAll);
                PageQueryAdapter1.PerformQuery();

                btnCreateNew.OnClientClick =string.Format("window.location='{0}';return false;",ResolveUrl(SiteUrls.Admin.EditUserInfo));
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnViewAll_Click(object sender,EventArgs e)
        {
            txtDepartment.Text = string.Empty;
            txtFullName.Text = string.Empty;
            txtLoginName.Text = string.Empty;
            PageQueryAdapter1.PerformQuery();
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            PredicateExpression Condition = new PredicateExpression();

            if (txtLoginName.Text.Trim().Length > 0)
            {
                Condition.Add(UserFields.LoginName % ("%" + txtLoginName.Text.Trim() + "%"));
            }
            if (txtFullName.Text.Trim().Length > 0)
            {
                Condition.Add(UserFields.FullName % ("%" + txtFullName.Text.Trim() + "%"));
            }
            if (chlRoles.SelectedValue.Trim().Length > 0)
            {
                var userRoleCollection = new UserRoleCollection();
                userRoleCollection.GetMulti(UserRoleFields.RoleId == new Guid(chlRoles.SelectedValue));

                var list = from item in userRoleCollection select item.UserId;

                var compareExpression = new FieldCompareRangePredicate(UserFields.UserId, false, list.ToArray());
                Condition.Add(compareExpression);
            }
            Condition.Add(UserFields.IsActived == true);
            e.Condition = Condition;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            e.RecordCount = SecurityBusiness.GetUserList((PredicateExpression)e.Condition).Count;
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridUsers.SortExpression)
            {
                case "LoginName":
                    sort = new SortExpression(UserFields.LoginName | (SortOperator)(int)gridUsers.SortDirection);
                    break;
                case "FullName":
                    sort = new SortExpression(UserFields.FullName | (SortOperator)(int)gridUsers.SortDirection);
                    break;
                case "Phone":
                    sort = new SortExpression(UserFields.Tel | (SortOperator)(int)gridUsers.SortDirection);
                    break;

                case "CreateDate":
                    sort = new SortExpression(UserFields.CreateDate | (SortOperator)(int)gridUsers.SortDirection);
                    break;
                case "Status":
                    sort = new SortExpression(UserFields.IsActived | (SortOperator)(int)gridUsers.SortDirection);
                    break;
                default:
                    sort = new SortExpression(UserFields.CreateDate | SortOperator.Descending);
                    break;
            }

            e.DataSource = SecurityBusiness.GetUserList((PredicateExpression)e.Condition, sort, e.PageSize, e.PageIndex + 1);
        }

        protected void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var data = (UserEntity)e.Row.DataItem;
                var rptRoles = (Repeater)e.Row.FindControl("rptRoles");
                if (rptRoles != null)
                {
                    var roles = SecurityBusiness.GetRoleListByUserID(data.UserId);

                    rptRoles.DataSource = roles;
                    rptRoles.DataBind();
                }
                e.Row.Attributes["UserId"] = data.UserId.ToString();
            }
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }


        protected void gridUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeleteItem":
                    if (SecurityBusiness.DeleteUser(int.Parse(e.CommandArgument.ToString())))
                    {
                        PageQueryAdapter1.PerformQuery();
                        base.Alert("Successfully deleted!");
                    }
                    break;
                case "ViewItem":
                    Response.Redirect("~/Admin/EditUserInfo.aspx?UserID=" + e.CommandArgument);
                    break;
                case "PermissionItem":
                    Response.Redirect("~/Admin/UserPermissionSet.aspx?UserID=" + e.CommandArgument);
                    break;
                    
            }
        }



    }
}
